package com.charmzenus.champion.utils;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class StringUtil {

    public static String urlAppendParam(String param, String value, String url) {
        if (param == null) {
            return url;
        }

        if (url == null) {
            return "";
        }

        if (url.indexOf("?") > 0) {
            return url + "&" + param + "=" + value;
        } else {
            return url + "?" + param + "=" + value;
        }
    }

    /**
     * unicode 转字符串
     */
    public static String unicodeToString(String unicode) {
        StringBuffer string = new StringBuffer();

        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);

            // 追加成string
            string.append((char) data);
        }

        return string.toString();
    }

    @SuppressLint("SimpleDateFormat")
    public static Date stringToDate(String str, String formatStr) {
        DateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            // Fri Feb 24 00:00:00 CST 2012  
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public static String parseTimeByPattern(String timeStr, String oldPattern, String newPattern) {
        String returnStr = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(oldPattern);

            // SimpleDateFormat的parse(String time)方法将String转换为Date
            Date date = simpleDateFormat.parse(timeStr);
            simpleDateFormat = new SimpleDateFormat(newPattern);

            // SimpleDateFormat的format(Date date)方法将Date转换为String
            returnStr = simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnStr;
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * 两位小数字符串转int *100
     *
     * @param str
     * @return
     */
    public static String multiplyHundred(String str) {
        String value = (long) (Double.parseDouble(str) * 100) + "";
        return value;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * @param str
     * @return
     */
    public static int stringToInt(String str, int defaultValue) {
        int value = defaultValue;
        try {
            value = Integer.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * @param str
     * @return
     */
    public static int stringToInt(String str) {
        return stringToInt(str, -1);
    }

    public static float stringToFloat(String str, float defaultValue) {
        float value = defaultValue;
        try {
            value = Float.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    public static float stringToFloat(String str) {
        return stringToFloat(str, -1);
    }

    public static double stringToDouble(String str) {
        double value = -1;
        try {
            value = Double.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    public static double stringToDoubleWithDefault(String str, double defaultData) {
        double value = defaultData;
        try {
            value = Double.valueOf(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * 获取指定key的intent传值
     *
     * @param key
     * @param activity
     * @return
     */
    public static String getStringFromBundle(String key, Activity activity) {
        if (key == null || key.equals("") || activity == null) {
            return "";
        }

        Intent intent = activity.getIntent();
        if (intent == null) {
            return "";
        }

        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            return "";
        }

        return bundle.getString(key);
    }

    /**
     * 字符串转MAC 地址
     *
     * @param str
     * @return
     */
    public static String Str2Mac(String str) {
        String mac = "";
        if (str != null && str.length() > 0) {
            for (int i = 0; i < str.length(); i++) {
                mac += str.charAt(i);
                if (i % 2 == 1 && i != str.length() - 1) {
                    mac += ":";
                }
            }
        }
        return mac;
    }

    /**
     * 根据jsonString获取指定的Json Object
     *
     * @param json
     * @param classOfT
     * @return
     */
    public static <T> T getObjFromJsonString(String json, Class<T> classOfT) {
        try {
            Gson gson = new Gson();
            T object = gson.fromJson(json, classOfT);

            return object;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static int getIntFromJson(String jsonStr, String key) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject.getInt(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }

    public static boolean getBooleanFromJson(String jsonStr, String key) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject.getBoolean(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static int getIntFromJson(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getInt(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public static String getStringFromJson(String jsonStr, String key) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String getStringFromJson(JSONObject jsonObj, String key) {
        try {
            return jsonObj.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static JSONObject getJSONObjFromJson(String jsonStr, String key) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            return jsonObject.getJSONObject(key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static <T> ArrayList<T> getArrayFromJson(String jsonStr, String key, Class<T> classOfT) {
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            ArrayList<T> list = new ArrayList<T>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);

                list.add(getObjFromJsonString(obj.toString(), classOfT));
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取String 避免空指针的情况
     */
    public static String getStringText(String text) {
        return TextUtils.isEmpty(text) ? "" : text;
    }

    /**
     * 获取String 避免空指针的情况
     */
    public static void setStringText(TextView textView, String text) {
        if (TextUtils.isEmpty(text) || text.equals("")) {
            return;
        }
        textView.setText(text);
    }

    /**
     * 小写 转大写
     *
     * @param text
     * @return
     */
    public static String lowerToUpper(String text) {
        String result = "";
        if (TextUtils.isEmpty(text)) {
            return result;
        }
        try {
            result = text.toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            result = text;
        }
        return result;
    }

}
